package com.api.shopapi.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.response.AlipayTradePayResponse;
import com.api.shopapi.entity.*;
import com.api.shopapi.entity.dto.*;
import com.api.shopapi.service.*;
import com.api.shopapi.util.AliPayUtils;
import com.api.shopapi.util.WechatPayUtils;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.util.TextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * 收银机
 * */

@RestController
@RequestMapping("AppCashier")
public class AppCashierController extends BaseController{

    @Autowired
    @Qualifier("UserServiceImplOne")
    private UserService userService;

    @Autowired
    @Qualifier("CashierServiceImplOne")
    private CashierService cashierService;

    @Autowired
    @Qualifier("BusinessServiceImplOne")
    private BusinessService businessService;

    @Autowired
    @Qualifier("GoodsClassGovernmentServiceImplOne")
    private GoodsClassGovernmentService goodsClassGovernmentService;

    @Autowired
    @Qualifier("GoodsServiceImplOne")
    private GoodsService goodsService;

    @Autowired
    @Qualifier("GoodsClassBusinessServiceImplOne")
    private GoodsClassBusinessService goodsClassBusinessService;

    @Autowired
    @Qualifier("GoodsPriceServiceImplOne")
    private GoodsPriceService goodsPriceService;

    @Autowired
    @Qualifier("GoodsSizeServiceImplOne")
    private GoodsSizeService goodsSizeService;

    @Autowired
    @Qualifier("GoodsColorServiceImplOne")
    private GoodsColorService goodsColorService;

    @Autowired
    @Qualifier("GoodsAttributeServiceImplOne")
    private GoodsAttributeService goodsAttributeService;




    @Autowired
    @Qualifier("CashierCartServiceImplOne")
    private CashierCartService cashierCartService;

    @Autowired
    @Qualifier("CashierCartBusinessServiceImplOne")
    private CashierCartBusinessService cashierCartBusinessService;

    @Autowired
    @Qualifier("CashierCartGoodsServiceImplOne")
    private CashierCartGoodsService cashierCartGoodsService;

    @Autowired
    @Qualifier("CashierCartGoodsPriceServiceImplOne")
    private CashierCartGoodsPriceService cashierCartGoodsPriceService;



    @Autowired
    @Qualifier("OrderServiceImplOne")
    private OrderService orderService;

    @Autowired
    @Qualifier("OrderBusinessServiceImplOne")
    private OrderBusinessService orderBusinessService;

    @Autowired
    @Qualifier("OrderGoodsServiceImplOne")
    private OrderGoodsService orderGoodsService;

    @Autowired
    @Qualifier("OrderTradenoServiceImplOne")
    private OrderTradenoService orderTradenoService;

    @Autowired
    @Qualifier("OrderBusinessTradenoRelServiceImplOne")
    private OrderBusinessTradenoRelService orderBusinessTradenoRelService;

    @Autowired
    @Qualifier("OrderServeServiceImplOne")
    private OrderServeService orderServeService;

    @Autowired
    @Qualifier("UserAddressServiceImplOne")
    private UserAddressService userAddressService;

    @Autowired
    @Qualifier("ElderlyServiceImplOne")
    private ElderlyService elderlyService;

    @Autowired
    @Qualifier("OrderGoodsPriceServiceImplOne")
    private OrderGoodsPriceService orderGoodsPriceService;

    @Autowired
    @Qualifier("UserPrinterServiceImplOne")
    private UserPrinterService userPrinterService;

    @Autowired
    @Qualifier("CashierLoginRecordServiceImplOne")
    private CashierLoginRecordService cashierLoginRecordService;




    //登录
    @RequestMapping(value = "Login",method = RequestMethod.POST)
    public MyResult Login(@RequestBody JSONObject jsonObject) {
        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String phone = jsonObject.getString("phone");
            String password = jsonObject.getString("password");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Cashier/Login");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            User user = userService.getByPhoneAndPassword(phone,getMD5Str(password));

            if(user == null){
                return failed("用户名或者密码错误");
            }

            if(user.getUserType()!=5){
                return failed("用户类型错误");
            }

            Cashier cashier = cashierService.getByUserCode(user.getUserCode());
            if(cashier == null){
                return failed("职工不存在");
            }

            Business business = businessService.getByBusinessCode(cashier.getBusinessCode());
            if(business == null){
                return failed("商家不存在");
            }


            List<CashierLoginRecord> listCashierLoginRecord =
                    cashierLoginRecordService.getListByUserCodeLoginStatus(user.getUserCode(),0);
            for(CashierLoginRecord cashierLoginRecord:listCashierLoginRecord){
                cashierLoginRecord.setLoginStatus(1);
                cashierLoginRecord.setEndTime(new Date());
                cashierLoginRecordService.update(cashierLoginRecord);
            }

            CashierLoginRecord cashierLoginRecord = new CashierLoginRecord();
            cashierLoginRecord.setCashierLoginRecordCode(UUID.randomUUID().toString());
            cashierLoginRecord.setUserCode(user.getUserCode());
            cashierLoginRecord.setStartTime(new Date());
            cashierLoginRecord.setEndTime(getInitTime());
            cashierLoginRecord.setLoginStatus(0);
            cashierLoginRecordService.insert(cashierLoginRecord);



            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("user",user);
            jsonObject1.put("cashier",cashier);
            jsonObject1.put("business",business);
            return success(jsonObject1);
        }catch (Exception e){
            insertError("/Cashier/Login",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }


    //退出
    @RequestMapping(value = "LoginOut",method = RequestMethod.POST)
    public MyResult LoginOut(@RequestBody JSONObject jsonObject) {
        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String userCode = jsonObject.getString("userCode");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Cashier/LoginOut");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            User user = userService.getByUserCode(userCode);


            List<CashierLoginRecord> listCashierLoginRecord =
                    cashierLoginRecordService.getListByUserCodeLoginStatus(user.getUserCode(),0);
            for(CashierLoginRecord cashierLoginRecord:listCashierLoginRecord){
                cashierLoginRecord.setLoginStatus(1);
                cashierLoginRecord.setEndTime(new Date());
                cashierLoginRecordService.update(cashierLoginRecord);
            }


            JSONObject jsonObject1 = new JSONObject();
            return success(jsonObject1);
        }catch (Exception e){
            insertError("/Cashier/LoginOut",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }



    //获取交接信息
    @RequestMapping(value = "HandoverInfo",method = RequestMethod.POST)
    public MyResult HandoverInfo(@RequestBody JSONObject jsonObject) {
        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String userCode = jsonObject.getString("userCode");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Cashier/HandoverInfo");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            User user = userService.getByUserCode(userCode);

            UserPrinter userPrinter = userPrinterService.getByUserCode(userCode);


            List<CashierLoginRecord> listCashierLoginRecord =
                    cashierLoginRecordService.getListByUserCodeLoginStatus(user.getUserCode(),0);
            if(listCashierLoginRecord.size() != 1){
                return  failed("登录记录错误");
            }

            CashierLoginRecord cashierLoginRecord = listCashierLoginRecord.get(0);

            Date startTime = cashierLoginRecord.getStartTime();
            Date endTime = new Date();

            List<OrderBusinessDTO> listOrderBusinessDTO = orderBusinessService.getListByUserCodeTableTime(userCode,
                    startTime,endTime);





            int allOrderNumber = listOrderBusinessDTO.size();
            int cashNumber = 0;
            int yueNumber = 0;
            int scanNumber = 0;

            double allPrice = 0.0;
            double cashPrice = 0.0;
            double yuePrice = 0.0;
            double scanPrice = 0.0;

            int failNumber = 0;

            for(OrderBusinessDTO orderBusinessDTO :listOrderBusinessDTO){

                if(orderBusinessDTO.getStatus() == 3){

                    if(orderBusinessDTO.getPayType().equals("cash")){
                        cashNumber++;
                        cashPrice += orderBusinessDTO.getPrice();
                    }
                    if(orderBusinessDTO.getPayType().equals("yue")){
                        yueNumber++;
                        yuePrice += orderBusinessDTO.getPrice();
                    }
                    if(orderBusinessDTO.getPayType().equals("wx") || orderBusinessDTO.getPayType().equals("ali")){
                        scanNumber++;
                        scanPrice += orderBusinessDTO.getPrice();
                    }


                }
                else{
                    failNumber++;
                }

            }

            allPrice = cashPrice+yuePrice+scanPrice;



            Date st = cashierLoginRecord.getStartTime();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            String stt = sdf.format(st);

            String printerStr = "---------交接班数据---------\n";
            printerStr += "收银："+user.getName()+"("+user.getPhone()+")\n";
            printerStr += "开始时间："+stt+"\n";
            printerStr += "结束时间：endTime\n";
            printerStr += "-----------------------\n";

            printerStr += "总金额："+allPrice+"\n";
            printerStr += "扫码金额："+scanPrice+"\n";
            printerStr += "会员金额："+yuePrice+"\n";
            printerStr += "现金金额："+cashPrice+"\n";

            printerStr += "-----------------------\n";

            printerStr += "总订单："+allOrderNumber+"\n";
            printerStr += "扫码订单："+scanNumber+"\n";
            printerStr += "会员订单："+yueNumber+"\n";
            printerStr += "现金订单："+cashNumber+"\n";
            printerStr += "异常订单："+failNumber+"\n";

            printerStr += "-----------------------\n";


            JSONObject jsonObject2 = new JSONObject();
            jsonObject2.put("allOrderNumber",allOrderNumber);
            jsonObject2.put("cashNumber",cashNumber);
            jsonObject2.put("yueNumber",yueNumber);
            jsonObject2.put("scanNumber",scanNumber);
            jsonObject2.put("failNumber",failNumber);

            jsonObject2.put("allPrice",allPrice);
            jsonObject2.put("cashPrice",cashPrice);
            jsonObject2.put("yuePrice",yuePrice);
            jsonObject2.put("scanPrice",scanPrice);



            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("user",user);
            jsonObject1.put("info",jsonObject2);
            jsonObject1.put("cashierLoginRecord",cashierLoginRecord);
            jsonObject1.put("printerStr",printerStr);
            jsonObject1.put("userPrinter",userPrinter);
            return success(jsonObject1);
        }catch (Exception e){
            insertError("/Cashier/HandoverInfo",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }




    //获取老人根据ic卡号
    @RequestMapping(value = "GetElderlyByIcCard",method = RequestMethod.POST)
    public MyResult GetElderlyByIcCard(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String userCode = jsonObject.getString("userCode");
            String icCard = jsonObject.getString("icCard").trim();

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Cashier/GetElderlyByIcCard");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Elderly elderly = elderlyService.getByIcCard(icCard);

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("elderly",elderly);
            return success(jsonObject1);
        }catch (Exception e){
            insertError("/AppCashier/GetElderlyByIcCard",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }

    //获取老人根据手机号
    @RequestMapping(value = "GetElderlyByPhone",method = RequestMethod.POST)
    public MyResult GetElderlyByPhone(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String userCode = jsonObject.getString("userCode");
            String phone = jsonObject.getString("phone").trim();

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Cashier/GetElderlyByPhone");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Elderly elderly = elderlyService.getByPhone(phone);

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("elderly",elderly);
            return success(jsonObject1);
        }catch (Exception e){
            insertError("/AppCashier/GetElderlyByPhone",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }



    //App错误信息上传
    @RequestMapping(value = "AppError", method = RequestMethod.POST)
    public MyResult AppError(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String info = jsonObject.getString("info");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Cashier/AppError");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            insertError("cashier app", "", info, "table");


            JSONObject jsonObject1 = new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/AppCashier/AppError", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }








    /**
     * goods class ------------------
     **/


    //根据商家查询一级商品分类
    @RequestMapping(value = "GetListByBusiness", method = RequestMethod.POST)
    public MyResult GetListByBusiness(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String businessCode = jsonObject.getString("businessCode");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Cashier/GetListByBusiness");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            List<GoodsClassBusinessDTO> listGoodsClassBusinessDTO = goodsClassBusinessService.getListByParentAll(businessCode,emptyUUID);


            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("listGoodsClassBusinessDTO",listGoodsClassBusinessDTO);
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/AppCashier/GetListByBusiness", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }

    //根据商家商品分类获取商品列表
    @RequestMapping(value = "ListToClassBusiness",method = RequestMethod.POST)
    public MyResult ListToClassBusiness(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String goodsClassBusinessCode = jsonObject.getString("goodsClassBusinessCode");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/AppCashier/ListToClassBusiness");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            List<GoodsDTO> goodsDTOPageInfo = goodsService.getByGoodsClassBusinessCode(goodsClassBusinessCode);
            for (GoodsDTO goodsDTO:goodsDTOPageInfo){

                List<GoodsPriceDTO> goodsPriceList =goodsPriceService.getByGoodsCode(goodsDTO.getGoodsCode());
                for (GoodsPriceDTO goodsPriceDTO:goodsPriceList){
                    if(goodsPriceDTO.getGoodsSizeCode()!=null && !goodsPriceDTO.getGoodsSizeCode().equals("")){
                        GoodsSize goodsSize =goodsSizeService.getByGoodsSizeCode(goodsPriceDTO.getGoodsSizeCode());
                        goodsPriceDTO.setGoodsSizeName(goodsSize.getName());
                    }else {
                        goodsPriceDTO.setGoodsSizeName("");
                    }
                    if(goodsPriceDTO.getGoodsColorCode()!=null && !goodsPriceDTO.getGoodsColorCode().equals("")){
                        GoodsColor goodsColor =goodsColorService.getByGoodsColorCode(goodsPriceDTO.getGoodsColorCode());
                        goodsPriceDTO.setGoodsColorName(goodsColor.getName());
                    }else {
                        goodsPriceDTO.setGoodsColorName("");
                    }
                }
                goodsDTO.setGoodsPriceDTOList(goodsPriceList);


                String goodsAttributeCodes=goodsDTO.getGoodsAttributeCodes();
                String[] goodsAttributeCodes1=goodsAttributeCodes.split(",");
                JSONArray attribute =new JSONArray();
                for (int i=0;i<goodsAttributeCodes1.length;i++){
                    if(goodsAttributeCodes1[i].equals("GoodsSizeCode")){
                        JSONObject jsonObject1 = new JSONObject();
                        List<GoodsSize> goodsSizeList=goodsSizeService.getAllList();
                        jsonObject1.put("attributeName","商品尺寸");
                        jsonObject1.put("attributeCode",goodsAttributeCodes1[i]);
                        jsonObject1.put("attributeList",goodsSizeList);
                        attribute.add(jsonObject1);
                    }
                    if(goodsAttributeCodes1[i].equals("GoodsColorCode")){
                        JSONObject jsonObject1 = new JSONObject();
                        List<GoodsColor> goodsColorList = goodsColorService.getList();
                        jsonObject1.put("attributeName","商品颜色");
                        jsonObject1.put("attributeCode",goodsAttributeCodes1[i]);
                        jsonObject1.put("attributeList",goodsColorList);
                        attribute.add(jsonObject1);
                    }
                }
                goodsDTO.setAttribute(attribute);

            }

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("listGoodsDTO",goodsDTOPageInfo);
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/AppCashier/ListToClassBusiness", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }

    //根据关键字搜索商品
    @RequestMapping(value = "GetListByKey", method = RequestMethod.POST)
    public MyResult GetListByKey(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String businessCode = jsonObject.getString("businessCode");
            String key = jsonObject.getString("key");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Cashier/GetListByKey");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            List<GoodsDTO> goodsDTOPageInfo = goodsService.getListByBusinessCodeKey(businessCode,key);
            for (GoodsDTO goodsDTO:goodsDTOPageInfo){

                List<GoodsPriceDTO> goodsPriceList =goodsPriceService.getByGoodsCode(goodsDTO.getGoodsCode());
                for (GoodsPriceDTO goodsPriceDTO:goodsPriceList){
                    if(goodsPriceDTO.getGoodsSizeCode()!=null && !goodsPriceDTO.getGoodsSizeCode().equals("")){
                        GoodsSize goodsSize =goodsSizeService.getByGoodsSizeCode(goodsPriceDTO.getGoodsSizeCode());
                        goodsPriceDTO.setGoodsSizeName(goodsSize.getName());
                    }else {
                        goodsPriceDTO.setGoodsSizeName("");
                    }
                    if(goodsPriceDTO.getGoodsColorCode()!=null && !goodsPriceDTO.getGoodsColorCode().equals("")){
                        GoodsColor goodsColor =goodsColorService.getByGoodsColorCode(goodsPriceDTO.getGoodsColorCode());
                        goodsPriceDTO.setGoodsColorName(goodsColor.getName());
                    }else {
                        goodsPriceDTO.setGoodsColorName("");
                    }
                }
                goodsDTO.setGoodsPriceDTOList(goodsPriceList);


                String goodsAttributeCodes=goodsDTO.getGoodsAttributeCodes();
                String[] goodsAttributeCodes1=goodsAttributeCodes.split(",");
                JSONArray attribute =new JSONArray();
                for (int i=0;i<goodsAttributeCodes1.length;i++){
                    if(goodsAttributeCodes1[i].equals("GoodsSizeCode")){
                        JSONObject jsonObject1 = new JSONObject();
                        List<GoodsSize> goodsSizeList=goodsSizeService.getAllList();
                        jsonObject1.put("attributeName","商品尺寸");
                        jsonObject1.put("attributeCode",goodsAttributeCodes1[i]);
                        jsonObject1.put("attributeList",goodsSizeList);
                        attribute.add(jsonObject1);
                    }
                    if(goodsAttributeCodes1[i].equals("GoodsColorCode")){
                        JSONObject jsonObject1 = new JSONObject();
                        List<GoodsColor> goodsColorList = goodsColorService.getList();
                        jsonObject1.put("attributeName","商品颜色");
                        jsonObject1.put("attributeCode",goodsAttributeCodes1[i]);
                        jsonObject1.put("attributeList",goodsColorList);
                        attribute.add(jsonObject1);
                    }
                }
                goodsDTO.setAttribute(attribute);

            }

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("listGoodsDTO",goodsDTOPageInfo);
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/AppCashier/GetListByKey", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }






    /**
     * Set------------------------
     * */

    //商家付款配置
    @RequestMapping(value = "GetBusinessSet", method = RequestMethod.POST)
    public MyResult GetBusinessSet(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String businessCode = jsonObject.getString("businessCode");
            String userCode = jsonObject.getString("userCode");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Cashier/GetBusinessSet");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }


            JSONObject jsonObject1 = new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/AppCashier/GetBusinessSet", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }

    //获取打印配置
    @RequestMapping(value = "GetUserPrinter", method = RequestMethod.POST)
    public MyResult GetUserPrinter(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String userCode = jsonObject.getString("userCode");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Cashier/GetUserPrinter");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            UserPrinter userPrinter = userPrinterService.getByUserCode(userCode);

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("userPrinter",userPrinter);
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/AppCashier/GetUserPrinter", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }

    //保存小票USB配置
    @RequestMapping(value = "SaveSmallTicketUSB", method = RequestMethod.POST)
    public MyResult SaveSmallTicketUSB(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String userCode = jsonObject.getString("userCode");
            String deviceInfo = jsonObject.getString("deviceInfo");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Cashier/SaveSmallTicketUSB");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            UserPrinter userPrinter = userPrinterService.getByUserCode(userCode);
            if(userPrinter == null){
                userPrinter = new UserPrinter();
                userPrinter.setUserPrinterCode(UUID.randomUUID().toString());
                userPrinter.setUserCode(userCode);
                userPrinter.setSmallTicketUSB(deviceInfo);
                userPrinterService.insert(userPrinter);
            }
            else{
                userPrinter.setSmallTicketUSB(deviceInfo);
                userPrinterService.update(userPrinter);
            }

            JSONObject jsonObject1 = new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/AppCashier/SaveSmallTicketUSB", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }

    //保存小票USB使用状态的配置
    @RequestMapping(value = "SaveSmallTicketUSBStatus", method = RequestMethod.POST)
    public MyResult SaveSmallTicketUSBStatus(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String userCode = jsonObject.getString("userCode");
            int status = jsonObject.getInteger("status");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Cashier/SaveSmallTicketUSBStatus");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            UserPrinter userPrinter = userPrinterService.getByUserCode(userCode);
            if(userPrinter == null){
                userPrinter = new UserPrinter();
                userPrinter.setUserPrinterCode(UUID.randomUUID().toString());
                userPrinter.setUserCode(userCode);
                userPrinter.setSmallTicketUSBStatus(status);
                userPrinterService.insert(userPrinter);
            }
            else{
                userPrinter.setSmallTicketUSBStatus(status);
                userPrinterService.update(userPrinter);
            }

            JSONObject jsonObject1 = new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/AppCashier/SaveSmallTicketUSBStatus", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }



    /**
     * cashier cart------------------
     **/

    //加入购物车
    @RequestMapping(value = "CartAdd",method = RequestMethod.POST)
    @Transactional
    public MyResult CartAdd(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String userCode = jsonObject.getString("userCode");

            String goodsCode = jsonObject.getString("goodsCode");
            int number = jsonObject.getInteger("number");

            //字符串数组，属性code数组
            JSONArray goodsAttributeCodesArg = jsonObject.getJSONArray("goodsAttributeCodesArg");
            //字符串数组，属性code对应的值
            JSONArray goodsAttributeCodesValueArg = jsonObject.getJSONArray("goodsAttributeCodesValueArg");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Cashier/CartAdd");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }


            double goodsPrice = GetPrice(goodsCode,goodsAttributeCodesArg,goodsAttributeCodesValueArg);


            User user11 = userService.getByUserCode(userCode);
            String governmentCode = user11.getGovernmentCode();


            Goods goods = goodsService.getByGoodsCode(goodsCode);
            String businessCode = goods.getBusinessCode();


            if(goods.getGoodsType() != 1){
                return failed("非普通类商品，不能添加");
            }


            CashierCart cart = cashierCartService.getByUserCodeGovernmentCode(userCode,governmentCode);
            if(cart == null){
                cart = new CashierCart();
                cart.setCashierCartCode(UUID.randomUUID().toString());
                cart.setUserCode(userCode);
                cart.setGovernmentCode(governmentCode);
                cashierCartService.insert(cart);
            }




            //check goods in cart or not
            Boolean isExist = false;
            List<CashierCartBusiness> listCartBusiness =
                    cashierCartBusinessService.getListByCashierCartCodeWillSubmit(cart.getCashierCartCode());
            for(CashierCartBusiness cartBusiness : listCartBusiness){
                List<CashierCartGoods> listCartGoods =
                        cashierCartGoodsService.getListByCashierCartBusinessCode(cartBusiness.getCashierCartBusinessCode());
                for(CashierCartGoods cartGoods : listCartGoods){
                    if(cartGoods.getGoodsCode().equals(goodsCode)){

                        Boolean b = hadCashierCartGoodsPriceExist(cartGoods.getCashierCartGoodsCode(),
                                goodsAttributeCodesArg,
                                goodsAttributeCodesValueArg);
                        if(b){
                            isExist = true;
                        }

                        break;
                    }
                }
            }



            if(!isExist){


                //cart business
                CashierCartBusiness cartBusinessSelected = null;
                //普通商品合并订单
                if(goods.getGoodsType() == 1){
                    for(CashierCartBusiness cartBusiness : listCartBusiness){
                        if(cartBusiness.getBusinessCode().equals(businessCode) && cartBusiness.getGoodsType() == 1 ){
                            cartBusinessSelected = cartBusiness;
                            break;
                        }
                    }
                }
                if(cartBusinessSelected == null){
                    cartBusinessSelected = new CashierCartBusiness();
                    cartBusinessSelected.setCashierCartBusinessCode(UUID.randomUUID().toString());
                    cartBusinessSelected.setCashierCartCode(cart.getCashierCartCode());
                    cartBusinessSelected.setBusinessCode(businessCode);
                    cartBusinessSelected.setUserCode(userCode);
                    cartBusinessSelected.setPrice(goodsPrice*number);
                    cartBusinessSelected.setGoodsType(goods.getGoodsType());

                    cartBusinessSelected.setType("WillSubmit");
                    cartBusinessSelected.setCreateTime(new Date());

                    cashierCartBusinessService.insert(cartBusinessSelected);
                }


                CashierCartGoods cartGoodsSelected = new CashierCartGoods();
                cartGoodsSelected.setCashierCartGoodsCode(UUID.randomUUID().toString());
                cartGoodsSelected.setCashierCartBusinessCode(cartBusinessSelected.getCashierCartBusinessCode());
                cartGoodsSelected.setGoodsCode(goodsCode);
                cartGoodsSelected.setNumber(number);
                cartGoodsSelected.setCreateTime(new Date());
                cartGoodsSelected.setAddedType("Added");
                cashierCartGoodsService.insert(cartGoodsSelected);


                CashierCartGoodsPrice cartGoodsPrice = new CashierCartGoodsPrice();
                cartGoodsPrice.setCashierCartGoodsPriceCode(UUID.randomUUID().toString());
                cartGoodsPrice.setCashierCartGoodsCode(cartGoodsSelected.getCashierCartGoodsCode());
                cartGoodsPrice.setPrice(goodsPrice);
                cartGoodsPrice.setGoodsSizeCode(getValueByKey("goodsSizeCode",goodsAttributeCodesArg,
                        goodsAttributeCodesValueArg));
                cartGoodsPrice.setGoodsColorCode(getValueByKey("goodsColorCode",goodsAttributeCodesArg,
                        goodsAttributeCodesValueArg));
                cashierCartGoodsPriceService.insert(cartGoodsPrice);


            }





            //update price
            double priceCart = 0;
            List<CashierCartBusiness> listCartBusiness2 =
                    cashierCartBusinessService.getListByCashierCartCode(cart.getCashierCartCode());
            for(CashierCartBusiness cartBusiness : listCartBusiness2){

                double priceBusiness = 0;
                List<CashierCartGoods> listCartGoods2 =
                        cashierCartGoodsService.getListByCashierCartBusinessCode(cartBusiness.getCashierCartBusinessCode());
                for(CashierCartGoods cartGoods:listCartGoods2){
                    CashierCartGoodsPrice cartGoodsPrice =
                            cashierCartGoodsPriceService.getByCashierCartGoodsCode(cartGoods.getCashierCartGoodsCode());
                    priceBusiness += cartGoodsPrice.getPrice() * cartGoods.getNumber();
                }
                cartBusiness.setPrice(priceBusiness);
                cashierCartBusinessService.update(cartBusiness);

                //priceCart += priceBusiness;
            }
            //cart.setPrice(priceCart);
            //cashierCartService.update(cart);



            JSONObject jsonObject1 = new JSONObject();
            return success(jsonObject1);
        }catch (Exception e){
            insertErrorForAffair("/AppCashier/CartAdd",jsonObject,e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return anomalous(getExceptionMsg(e));
        }

    }

    //加入购物车，无码商品
    @RequestMapping(value = "CartAddWuMa",method = RequestMethod.POST)
    @Transactional
    public MyResult CartAddWuMa(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String userCode = jsonObject.getString("userCode");
            String businessCode = jsonObject.getString("businessCode");

            double price = jsonObject.getDouble("price");
            int number = 1;

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Cashier/CartAddWuMa");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            User user11 = userService.getByUserCode(userCode);
            String governmentCode = user11.getGovernmentCode();

            CashierCart cart = cashierCartService.getByUserCodeGovernmentCode(userCode,governmentCode);
            if(cart == null){
                cart = new CashierCart();
                cart.setCashierCartCode(UUID.randomUUID().toString());
                cart.setUserCode(userCode);
                cart.setGovernmentCode(governmentCode);
                cashierCartService.insert(cart);
            }


            //check goods in cart or not
            Boolean isExist = false;
            List<CashierCartBusiness> listCartBusiness =
                    cashierCartBusinessService.getListByCashierCartCodeWillSubmit(cart.getCashierCartCode());

            if(!isExist){


                //cart business
                CashierCartBusiness cartBusinessSelected = null;
                for(CashierCartBusiness cartBusiness : listCartBusiness){
                    if(cartBusiness.getBusinessCode().equals(businessCode)){
                        cartBusinessSelected = cartBusiness;
                        break;
                    }
                }

                if(cartBusinessSelected == null){
                    cartBusinessSelected = new CashierCartBusiness();
                    cartBusinessSelected.setCashierCartBusinessCode(UUID.randomUUID().toString());
                    cartBusinessSelected.setCashierCartCode(cart.getCashierCartCode());
                    cartBusinessSelected.setBusinessCode(businessCode);
                    cartBusinessSelected.setUserCode(userCode);
                    cartBusinessSelected.setPrice(price);
                    cartBusinessSelected.setGoodsType(1);

                    cartBusinessSelected.setType("WillSubmit");
                    cartBusinessSelected.setCreateTime(new Date());

                    cashierCartBusinessService.insert(cartBusinessSelected);
                }


                CashierCartGoods cartGoodsSelected = new CashierCartGoods();
                cartGoodsSelected.setCashierCartGoodsCode(UUID.randomUUID().toString());
                cartGoodsSelected.setCashierCartBusinessCode(cartBusinessSelected.getCashierCartBusinessCode());
                cartGoodsSelected.setGoodsCode(UUID.randomUUID().toString());
                cartGoodsSelected.setNumber(number);
                cartGoodsSelected.setCreateTime(new Date());
                cartGoodsSelected.setAddedType("WuMa");
                cashierCartGoodsService.insert(cartGoodsSelected);


                CashierCartGoodsPrice cartGoodsPrice = new CashierCartGoodsPrice();
                cartGoodsPrice.setCashierCartGoodsPriceCode(UUID.randomUUID().toString());
                cartGoodsPrice.setCashierCartGoodsCode(cartGoodsSelected.getCashierCartGoodsCode());
                cartGoodsPrice.setPrice(price);
                cashierCartGoodsPriceService.insert(cartGoodsPrice);


            }





            //update price
            double priceCart = 0;
            List<CashierCartBusiness> listCartBusiness2 =
                    cashierCartBusinessService.getListByCashierCartCode(cart.getCashierCartCode());
            for(CashierCartBusiness cartBusiness : listCartBusiness2){

                double priceBusiness = 0;
                List<CashierCartGoods> listCartGoods2 =
                        cashierCartGoodsService.getListByCashierCartBusinessCode(cartBusiness.getCashierCartBusinessCode());
                for(CashierCartGoods cartGoods:listCartGoods2){
                    CashierCartGoodsPrice cartGoodsPrice =
                            cashierCartGoodsPriceService.getByCashierCartGoodsCode(cartGoods.getCashierCartGoodsCode());
                    priceBusiness += cartGoodsPrice.getPrice() * cartGoods.getNumber();
                }
                cartBusiness.setPrice(priceBusiness);
                cashierCartBusinessService.update(cartBusiness);

                //priceCart += priceBusiness;
            }
            //cart.setPrice(priceCart);
            //cashierCartService.update(cart);



            JSONObject jsonObject1 = new JSONObject();
            return success(jsonObject1);
        }catch (Exception e){
            insertErrorForAffair("/AppCashier/CartAddWuMa",jsonObject,e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return anomalous(getExceptionMsg(e));
        }

    }


    //待提交的购物车
    @RequestMapping(value = "CartWillSubmit",method = RequestMethod.POST)
    public MyResult CartWillSubmit(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String userCode = jsonObject.getString("userCode");
            String businessCode = jsonObject.getString("businessCode");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Cashier/CartWillSubmit");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }


            CashierCartBusiness cartBusiness =
                    cashierCartBusinessService.getByBusinessCodeUserCodeWillSubmit(businessCode,userCode);

            CashierCartBusinessDTO cartBusinessDTO = mapper.map(cartBusiness,CashierCartBusinessDTO.class);


            if(cartBusinessDTO != null){

                Business business = businessService.getByBusinessCode(cartBusinessDTO.getBusinessCode());
                User user = userService.getByUserCode(business.getUserCode());
                cartBusinessDTO.setBusinessName(user.getName());

                List<CashierCartGoods> listCartGoods =
                        cashierCartGoodsService.getListByCashierCartBusinessCode(cartBusinessDTO.getCashierCartBusinessCode());
                List<CashierCartGoodsDTO> listCartGoodsDTO = new ArrayList<>();
                for(CashierCartGoods cartGoods:listCartGoods){
                    CashierCartGoodsDTO cartGoodsDTO = mapper.map(cartGoods,CashierCartGoodsDTO.class);
                    listCartGoodsDTO.add(cartGoodsDTO);
                }

                for(CashierCartGoodsDTO cartGoodsDTO:listCartGoodsDTO){

                    if(cartGoodsDTO.getAddedType().equals("Added")){
                        Goods goods = goodsService.getByGoodsCode(cartGoodsDTO.getGoodsCode());
                        cartGoodsDTO.setGoodsName(goods.getName());
                        cartGoodsDTO.setGoodsHead(goods.getHead1());
                    }
                    if(cartGoodsDTO.getAddedType().equals("WuMa")){
                        cartGoodsDTO.setGoodsName("无码");
                        cartGoodsDTO.setGoodsHead("");
                    }

                    CashierCartGoodsPrice cartGoodsPrice =
                            cashierCartGoodsPriceService.getByCashierCartGoodsCode(cartGoodsDTO.getCashierCartGoodsCode());
                    CashierCartGoodsPriceDTO cashierCartGoodsPriceDTO = mapper.map(cartGoodsPrice,
                            CashierCartGoodsPriceDTO.class);
                    if(cartGoodsPrice.getGoodsSizeCode()!=null && !cartGoodsPrice.getGoodsSizeCode().equals("")){
                        GoodsSize goodsSize =goodsSizeService.getByGoodsSizeCode(cartGoodsPrice.getGoodsSizeCode());
                        cashierCartGoodsPriceDTO.setGoodsSizeName(goodsSize.getName());
                    }
                    if(cartGoodsPrice.getGoodsColorCode()!=null && !cartGoodsPrice.getGoodsColorCode().equals("")){
                        GoodsColor goodsColor =goodsColorService.getByGoodsColorCode(cartGoodsPrice.getGoodsColorCode());
                        cashierCartGoodsPriceDTO.setGoodsColorName(goodsColor.getName());
                    }
                    cartGoodsDTO.setCashierCartGoodsPriceDTO(cashierCartGoodsPriceDTO);

                }
                cartBusinessDTO.setListCashierCartGoodsDTO(listCartGoodsDTO);

            }

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("cartBusinessDTO",cartBusinessDTO);
            return success(jsonObject1);
        }catch (Exception e){
            insertError("/AppCashier/CartWillSubmit",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }






    //修改购物车中商品数量
    @RequestMapping(value = "CartNumberChange",method = RequestMethod.POST)
    public MyResult CartNumberChange(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String userCode = jsonObject.getString("userCode");

            String cashierCartGoodsCode = jsonObject.getString("cashierCartGoodsCode");
            int number = jsonObject.getInteger("number");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Cashier/CartNumberChange");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            User user11 = userService.getByUserCode(userCode);
            String governmentCode = user11.getGovernmentCode();


            //cart goods
            CashierCartGoods cartGoods =
                    cashierCartGoodsService.getByCashierCartGoodsCode(cashierCartGoodsCode);

            Goods goods = goodsService.getByGoodsCode(cartGoods.getGoodsCode());
            if(goods.getStock() < number + goods.getSaleCount()){
                //return failed("库存不足");
            }
            cartGoods.setNumber(number);
            cashierCartGoodsService.update(cartGoods);




            CashierCart cart = cashierCartService.getByUserCodeGovernmentCode(userCode,governmentCode);

            //update price
            double priceCart = 0;
            List<CashierCartBusiness> listCartBusiness2 =
                    cashierCartBusinessService.getListByCashierCartCode(cart.getCashierCartCode());
            for(CashierCartBusiness cartBusiness : listCartBusiness2){

                double priceBusiness = 0;
                List<CashierCartGoods> listCartGoods2 =
                        cashierCartGoodsService.getListByCashierCartBusinessCode(cartBusiness.getCashierCartBusinessCode());
                for(CashierCartGoods cartGoods2:listCartGoods2){
                    CashierCartGoodsPrice cartGoodsPrice2 =
                            cashierCartGoodsPriceService.getByCashierCartGoodsCode(cartGoods2.getCashierCartGoodsCode());
                    priceBusiness += cartGoodsPrice2.getPrice() * cartGoods2.getNumber();
                }
                cartBusiness.setPrice(priceBusiness);
                cashierCartBusinessService.update(cartBusiness);

                //priceCart += priceBusiness;
            }
            //cart.setPrice(priceCart);
            //cartService.update(cart);



            JSONObject jsonObject1 = new JSONObject();
            return success(jsonObject1);
        }catch (Exception e){
            insertError("/AppCashier/CartNumberChange",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }

    //删除购物车中商品
    @RequestMapping(value = "CartItemDelete",method = RequestMethod.POST)
    @Transactional
    public MyResult CartItemDelete(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String userCode = jsonObject.getString("userCode");
            String cashierCartGoodsCode = jsonObject.getString("cashierCartGoodsCode");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Cashier/CartItemDelete");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            User user11 = userService.getByUserCode(userCode);
            String governmentCode = user11.getGovernmentCode();


            CashierCartGoods cartGoods = cashierCartGoodsService.getByCashierCartGoodsCode(cashierCartGoodsCode);
            cashierCartGoodsService.delete(cashierCartGoodsCode);
            cashierCartGoodsPriceService.delete(cashierCartGoodsCode);


            List<CashierCartGoods> listCarGoods =
                    cashierCartGoodsService.getListByCashierCartBusinessCode(cartGoods.getCashierCartBusinessCode());
            if(listCarGoods.size() == 0){
                cashierCartBusinessService.delete(cartGoods.getCashierCartBusinessCode());
            }


            CashierCart cart = cashierCartService.getByUserCodeGovernmentCode(userCode,governmentCode);

            //update price
            double priceCart = 0;
            List<CashierCartBusiness> listCartBusiness2 =
                    cashierCartBusinessService.getListByCashierCartCode(cart.getCashierCartCode());
            for(CashierCartBusiness cartBusiness : listCartBusiness2){

                double priceBusiness = 0;
                List<CashierCartGoods> listCartGoods2 =
                        cashierCartGoodsService.getListByCashierCartBusinessCode(cartBusiness.getCashierCartBusinessCode());
                for(CashierCartGoods cartGoods2:listCartGoods2){
                    CashierCartGoodsPrice cartGoodsPrice2 =
                            cashierCartGoodsPriceService.getByCashierCartGoodsCode(cartGoods2.getCashierCartGoodsCode());
                    priceBusiness += cartGoodsPrice2.getPrice() * cartGoods2.getNumber();
                }
                cartBusiness.setPrice(priceBusiness);
                cashierCartBusinessService.update(cartBusiness);

                //priceCart += priceBusiness;
            }
            //cart.setPrice(priceCart);
            //cartService.update(cart);



            JSONObject jsonObject1 = new JSONObject();
            return success(jsonObject1);
        }catch (Exception e){
            insertErrorForAffair("/AppCashier/CartItemDelete!",jsonObject,e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return anomalous(getExceptionMsg(e));
        }

    }

    //清空购物车
    @RequestMapping(value = "CartClear",method = RequestMethod.POST)
    public MyResult CartClear(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String userCode = jsonObject.getString("userCode");
            String businessCode = jsonObject.getString("businessCode");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Cashier/CartClear");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }


            CashierCartBusiness cartBusiness =
                    cashierCartBusinessService.getByBusinessCodeUserCodeWillSubmit(businessCode,userCode);
            cashierCartBusinessService.delete(cartBusiness.getCashierCartBusinessCode());

            List<CashierCartGoods> listCartGoods =
                    cashierCartGoodsService.getListByCashierCartBusinessCode(cartBusiness.getCashierCartBusinessCode());
            for(CashierCartGoods cartGoods:listCartGoods){
                cashierCartGoodsService.delete(cartGoods.getCashierCartGoodsCode());

                CashierCartGoodsPrice cashierCartGoodsPrice =
                        cashierCartGoodsPriceService.getByCashierCartGoodsCode(cartGoods.getCashierCartGoodsCode());
                cashierCartGoodsPriceService.delete(cashierCartGoodsPrice.getCashierCartGoodsPriceCode());

            }

            JSONObject jsonObject1 = new JSONObject();
            return success(jsonObject1);
        }catch (Exception e){
            insertError("/AppCashier/CartClear",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }


    //挂起中的购物车
    @RequestMapping(value = "CartWaitSubmit",method = RequestMethod.POST)
    public MyResult CartWaitSubmit(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String userCode = jsonObject.getString("userCode");
            String businessCode = jsonObject.getString("businessCode");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Cashier/CartWaitSubmit");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            List<CashierCartBusiness> listCashierCartBusiness =
                    cashierCartBusinessService.getListByBusinessCodeUserCodeWaitSubmit(businessCode,userCode);

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("listCashierCartBusiness",listCashierCartBusiness);
            return success(jsonObject1);
        }catch (Exception e){
            insertError("/AppCashier/CartWaitSubmit",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }

    //挂单
    @RequestMapping(value = "CartGuaDan",method = RequestMethod.POST)
    public MyResult CartGuaDan(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String userCode = jsonObject.getString("userCode");
            String businessCode = jsonObject.getString("businessCode");

            String guaDanRen = jsonObject.getString("guaDanRen");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Cashier/CartGuaDan");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            CashierCartBusiness cashierCartBusiness =
                    cashierCartBusinessService.getByBusinessCodeUserCodeWillSubmit(businessCode,userCode);
            cashierCartBusiness.setType("WaitSubmit");
            cashierCartBusiness.setGuaDanRen(guaDanRen);
            cashierCartBusinessService.update(cashierCartBusiness);

            JSONObject jsonObject1 = new JSONObject();
            return success(jsonObject1);
        }catch (Exception e){
            insertError("/AppCashier/CartGuaDan",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }

    //取单
    @RequestMapping(value = "CartQuDan",method = RequestMethod.POST)
    public MyResult CartQuDan(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String cashierCartBusinessCode = jsonObject.getString("cashierCartBusinessCode");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Cashier/CartQuDan");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            CashierCartBusiness cashierCartBusiness =
                    cashierCartBusinessService.getByCashierCartBusinessCode(cashierCartBusinessCode);
            cashierCartBusiness.setType("WillSubmit");
            cashierCartBusiness.setGuaDanRen("");
            cashierCartBusinessService.update(cashierCartBusiness);

            JSONObject jsonObject1 = new JSONObject();
            return success(jsonObject1);
        }catch (Exception e){
            insertError("/AppCashier/CartQuDan",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }




    /**
     * cashier submit------------------
     * */


    //收银员提交（现金）
    @RequestMapping(value = "CartSubmitCash",method = RequestMethod.POST)
    @Transactional
    public MyResult CartSubmitCash(@RequestBody JSONObject jsonObject, HttpServletRequest request) {

        try{

            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String userCode = jsonObject.getString("userCode");
            String businessCode = jsonObject.getString("businessCode");


            String payType = jsonObject.getString("payType");
            String clientType = "table";

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Cashier/CartSubmitCash");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            User user11 = userService.getByUserCode(userCode);
            String governmentCode = user11.getGovernmentCode();



            //支付交易号
            String tradeno = "sn"+ getTimeStrSSS()+ new Random().nextInt(100000);



            CashierCart cart = cashierCartService.getByUserCodeGovernmentCode(userCode,governmentCode);
            if(cart == null){
                return failed("购物车为空");
            }

            CashierCartBusiness cashierCartBusinessSelected = null;
            List<CashierCartBusiness> listCartBusiness =
                    cashierCartBusinessService.getListByCashierCartCodeWillSubmit(cart.getCashierCartCode());
            for(CashierCartBusiness cartBusiness : listCartBusiness){
                if(cartBusiness.getBusinessCode().equals(businessCode)){
                    cashierCartBusinessSelected = cartBusiness;
                }
            }
            if(cashierCartBusinessSelected == null){
                return failed("订单为空");
            }


            //订单的支付交易号
            OrderTradeno orderTradeno = new OrderTradeno();
            orderTradeno.setOrderTradenoCode(UUID.randomUUID().toString());
            orderTradeno.setUserCode(userCode);
            orderTradeno.setTradeNo(tradeno);
            orderTradeno.setPrice(cashierCartBusinessSelected.getPrice());
            orderTradeno.setCreateTime(new Date());
            orderTradeno.setStatus(0);
            orderTradeno.setPayType(payType);
            orderTradeno.setClientType(clientType);
            orderTradenoService.insert(orderTradeno);


            //订单
            Order order = new Order();
            order.setOrderCode(UUID.randomUUID().toString());
            order.setUserCode(userCode);
            order.setPrice(cashierCartBusinessSelected.getPrice());
            order.setCreateTime(new Date());
            order.setReceiveName("");
            order.setReceivePhone("");
            order.setReceiveArea("");
            order.setReceiveAreaPath("");
            order.setReceiveAddress("");
            order.setGovernmentCode(governmentCode);
            order.setCashierElderlyCode(emptyUUID);
            orderService.insert(order);




            String goodsNames="";
            String goodsNameMores="";

            for(CashierCartBusiness cartBusiness : listCartBusiness){
                if(cartBusiness.getBusinessCode().equals(businessCode)){

                    cashierCartBusinessService.delete(cartBusiness.getCashierCartBusinessCode());


                    Business business = businessService.getByBusinessCode(cartBusiness.getBusinessCode());
                    User userBusiness = userService.getByUserCode(business.getUserCode());

                    //订单流水号
                    String serialNumber = getTimeStr() +user11.getPhone()+ new Random().nextInt(10);

                    //订单的商家
                    OrderBusiness orderBusiness = new OrderBusiness();
                    orderBusiness.setOrderBusinessCode(UUID.randomUUID().toString());
                    orderBusiness.setOrderCode(order.getOrderCode());
                    orderBusiness.setBusinessCode(cartBusiness.getBusinessCode());
                    orderBusiness.setBusinessName(userBusiness.getName());
                    orderBusiness.setPrice(cartBusiness.getPrice());
                    orderBusiness.setCreateTime(new Date());
                    orderBusiness.setOrderType(cartBusiness.getGoodsType());
                    orderBusiness.setStatus(0);
                    orderBusiness.setSerialNumber(serialNumber);
                    orderBusiness.setPayType(payType);

                    orderBusiness.setOrderFrom("table");

                    orderBusinessService.insert(orderBusiness);


                    //商家的订单与支付交易号关联
                    OrderBusinessTradenoRel orderBusinessTradenoRel = new OrderBusinessTradenoRel();
                    orderBusinessTradenoRel.setOrderBusinessTradeNoRelCode(UUID.randomUUID().toString());
                    orderBusinessTradenoRel.setTradeNo(tradeno);
                    orderBusinessTradenoRel.setOrderBusinessCode(orderBusiness.getOrderBusinessCode());
                    orderBusinessTradenoRelService.insert(orderBusinessTradenoRel);



                    double priceOrder = orderBusiness.getPrice();

                    List<CashierCartGoods> listCartGoods =
                            cashierCartGoodsService.getListByCashierCartBusinessCode(cartBusiness.getCashierCartBusinessCode());
                    for(CashierCartGoods cartGoods : listCartGoods){
                        cashierCartGoodsService.delete(cartGoods.getCashierCartGoodsCode());


                        String goodsName = "";
                        String goodsNameMore = "";
                        String goodsHead = "";

                        if(cartGoods.getAddedType().equals("Added")){
                            Goods goods = goodsService.getByGoodsCode(cartGoods.getGoodsCode());
                            goodsName = goods.getName();
                            goodsNameMore = goods.getNameMore();
                            goodsHead = goods.getHead1();

                            //增加库存
                            if(goods.getStock() > goods.getSaleCount()+cartGoods.getNumber()){
                                goods.setSaleCount(goods.getSaleCount()+cartGoods.getNumber());
                                goodsService.update(goods);
                            }

                        }

                        goodsNames +=goodsName+",";
                        goodsNameMores +=goodsNameMore+",";

                        CashierCartGoodsPrice cartGoodsPrice =
                                cashierCartGoodsPriceService.getByCashierCartGoodsCode(cartGoods.getCashierCartGoodsCode());



                        //订单的商品
                        OrderGoods orderGoods = new OrderGoods();
                        orderGoods.setOrderGoodsCode(UUID.randomUUID().toString());
                        orderGoods.setOrderBusinessCode(orderBusiness.getOrderBusinessCode());
                        orderGoods.setGoodsCode(cartGoods.getGoodsCode());
                        orderGoods.setNumber(cartGoods.getNumber());
                        orderGoods.setPlanTime(new Date());
                        orderGoods.setGoodsName(goodsName);
                        orderGoods.setGoodsPrice(cartGoodsPrice.getPrice());
                        orderGoods.setGoodsImg(goodsHead);
                        orderGoodsService.insert(orderGoods);



                        //订单商品的属性
                        OrderGoodsPrice orderGoodsPrice = new OrderGoodsPrice();
                        orderGoodsPrice.setOrderGoodsPriceCode(UUID.randomUUID().toString());
                        orderGoodsPrice.setOrderGoodsCode(orderGoods.getOrderGoodsCode());
                        orderGoodsPrice.setPrice(cartGoodsPrice.getPrice());

                        if(cartGoods.getAddedType().equals("Added")){
                            String goodsSizeCode = cartGoodsPrice.getGoodsSizeCode();
                            String goodsSizeName = "";
                            if(!goodsSizeCode.equals("")){
                                goodsSizeName = goodsSizeService.getByGoodsSizeCode(goodsSizeCode).getName();
                            }
                            orderGoodsPrice.setGoodsSizeCode(goodsSizeCode);
                            orderGoodsPrice.setGoodsSizeName(goodsSizeName);

                            String goodsColorCode =cartGoodsPrice.getGoodsColorCode();
                            String goodsColorName = "";
                            if(!goodsColorCode.equals("")){
                                goodsColorName = goodsColorService.getByGoodsColorCode(goodsColorCode).getName();
                            }
                            orderGoodsPrice.setGoodsColorCode(goodsColorCode);
                            orderGoodsPrice.setGoodsColorName(goodsColorName);
                        }

                        orderGoodsPriceService.insert(orderGoodsPrice);


                    }

                }
            }

            goodsNames=goodsNames.substring(0,goodsNames.length()-1);
            goodsNameMores=goodsNameMores.substring(0,goodsNameMores.length()-1);

            if (goodsNames.length()>256){
                goodsNames=goodsNames.substring(0,256);
            }
            if (goodsNameMores.length()>1000){
                goodsNames=goodsNameMores.substring(0,1000);
            }



            if(payType.equals("cash")){

                //修改总订单的支付状态
                OrderTradeno orderTradeno1 = orderTradenoService.getByTradeno(tradeno);
                if(orderTradeno1.getStatus() == 0){
                    orderTradeno1.setStatus(1);
                    orderTradenoService.update(orderTradeno1);
                }


                //交易号关联的订单
                List<OrderBusinessTradenoRel> listOrderBusinessTradeoRel =
                        orderBusinessTradenoRelService.getListByTradeno(tradeno);

                //修改订单的状态
                for(OrderBusinessTradenoRel orderBusinessTradenoRel2:listOrderBusinessTradeoRel){
                    OrderBusiness orderBusiness2 =
                            orderBusinessService.getByOrderBusinessCode(orderBusinessTradenoRel2.getOrderBusinessCode());
                    if(orderBusiness2.getStatus() == 0){
                        orderBusiness2.setStatus(3);
                        orderBusiness2.setPayTime(new Date());
                        orderBusiness2.setCompleteTime(new Date());
                        orderBusinessService.update(orderBusiness2);
                    }
                }

            }



            UserPrinter userPrinter = userPrinterService.getByUserCode(userCode);
            String printerStr = getPrinterStr(order.getOrderCode());


            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("payType",payType);

            jsonObject1.put("printerStr",printerStr);
            jsonObject1.put("userPrinter",userPrinter);
            return success(jsonObject1);
        }catch (Exception e){
            insertErrorForAffair("/AppCashier/CartSubmitCash",jsonObject,e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return anomalous(getExceptionMsg(e));
        }

    }


    //收银员提交（余额）
    @RequestMapping(value = "CartSubmitMember",method = RequestMethod.POST)
    @Transactional
    public MyResult CartSubmitMember(@RequestBody JSONObject jsonObject) {

        try{

            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String userCode = jsonObject.getString("userCode");
            String businessCode = jsonObject.getString("businessCode");


            String payType = jsonObject.getString("payType");
            String clientType = "table";
            String elderlyCode = jsonObject.getString("elderlyCode");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Cashier/CartSubmitMember");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            User user11 = userService.getByUserCode(userCode);
            String governmentCode = user11.getGovernmentCode();

            Elderly elderly = elderlyService.getByElderlyCode(elderlyCode);


            //支付交易号
            String tradeno = "sn"+ getTimeStrSSS()+ new Random().nextInt(100000);


            CashierCart cart = cashierCartService.getByUserCodeGovernmentCode(userCode,governmentCode);
            if(cart == null){
                return failed("购物车为空");
            }

            CashierCartBusiness cashierCartBusinessSelected = null;
            List<CashierCartBusiness> listCartBusiness =
                    cashierCartBusinessService.getListByCashierCartCodeWillSubmit(cart.getCashierCartCode());
            for(CashierCartBusiness cartBusiness : listCartBusiness){
                if(cartBusiness.getBusinessCode().equals(businessCode)){
                    cashierCartBusinessSelected = cartBusiness;
                }
            }
            if(cashierCartBusinessSelected == null){
                return failed("订单为空");
            }
            if(elderly.getBalance()<cashierCartBusinessSelected.getPrice()){
                return failed("余额不足");
            }



            //订单的支付交易号
            OrderTradeno orderTradeno = new OrderTradeno();
            orderTradeno.setOrderTradenoCode(UUID.randomUUID().toString());
            orderTradeno.setUserCode(userCode);
            orderTradeno.setTradeNo(tradeno);
            orderTradeno.setPrice(cashierCartBusinessSelected.getPrice());
            orderTradeno.setCreateTime(new Date());
            orderTradeno.setStatus(0);
            orderTradeno.setPayType(payType);
            orderTradeno.setClientType(clientType);
            orderTradenoService.insert(orderTradeno);


            //订单
            Order order = new Order();
            order.setOrderCode(UUID.randomUUID().toString());
            order.setUserCode(userCode);
            order.setPrice(cashierCartBusinessSelected.getPrice());
            order.setCreateTime(new Date());
            order.setReceiveName("");
            order.setReceivePhone("");
            order.setReceiveArea("");
            order.setReceiveAreaPath("");
            order.setReceiveAddress("");
            order.setGovernmentCode(governmentCode);
            order.setCashierElderlyCode(elderlyCode);
            orderService.insert(order);



            for(CashierCartBusiness cartBusiness : listCartBusiness){
                if(cartBusiness.getBusinessCode().equals(businessCode)){

                    cashierCartBusinessService.delete(cartBusiness.getCashierCartBusinessCode());


                    Business business = businessService.getByBusinessCode(cartBusiness.getBusinessCode());
                    User userBusiness = userService.getByUserCode(business.getUserCode());


                    //订单流水号
                    String serialNumber = getTimeStr() +user11.getPhone()+ new Random().nextInt(10);

                    //订单的商家
                    OrderBusiness orderBusiness = new OrderBusiness();
                    orderBusiness.setOrderBusinessCode(UUID.randomUUID().toString());
                    orderBusiness.setOrderCode(order.getOrderCode());
                    orderBusiness.setBusinessCode(cartBusiness.getBusinessCode());
                    orderBusiness.setBusinessName(userBusiness.getName());
                    orderBusiness.setPrice(cartBusiness.getPrice());
                    orderBusiness.setCreateTime(new Date());
                    orderBusiness.setOrderType(cartBusiness.getGoodsType());
                    orderBusiness.setStatus(0);
                    orderBusiness.setSerialNumber(serialNumber);
                    orderBusiness.setPayType(payType);

                    orderBusiness.setOrderFrom("table");

                    orderBusinessService.insert(orderBusiness);


                    //商家的订单与支付交易号关联
                    OrderBusinessTradenoRel orderBusinessTradenoRel = new OrderBusinessTradenoRel();
                    orderBusinessTradenoRel.setOrderBusinessTradeNoRelCode(UUID.randomUUID().toString());
                    orderBusinessTradenoRel.setTradeNo(tradeno);
                    orderBusinessTradenoRel.setOrderBusinessCode(orderBusiness.getOrderBusinessCode());
                    orderBusinessTradenoRelService.insert(orderBusinessTradenoRel);



                    double priceOrder = orderBusiness.getPrice();

                    List<CashierCartGoods> listCartGoods =
                            cashierCartGoodsService.getListByCashierCartBusinessCode(cartBusiness.getCashierCartBusinessCode());
                    for(CashierCartGoods cartGoods : listCartGoods){
                        cashierCartGoodsService.delete(cartGoods.getCashierCartGoodsCode());


                        String goodsName = "";
                        String goodsNameMore = "";
                        String goodsHead = "";

                        if(cartGoods.getAddedType().equals("Added")){
                            Goods goods = goodsService.getByGoodsCode(cartGoods.getGoodsCode());
                            goodsName = goods.getName();
                            goodsNameMore = goods.getNameMore();
                            goodsHead = goods.getHead1();

                            //增加库存
                            if(goods.getStock() > goods.getSaleCount()+cartGoods.getNumber()){
                                goods.setSaleCount(goods.getSaleCount()+cartGoods.getNumber());
                                goodsService.update(goods);
                            }

                        }

                        CashierCartGoodsPrice cartGoodsPrice =
                                cashierCartGoodsPriceService.getByCashierCartGoodsCode(cartGoods.getCashierCartGoodsCode());



                        //订单的商品
                        OrderGoods orderGoods = new OrderGoods();
                        orderGoods.setOrderGoodsCode(UUID.randomUUID().toString());
                        orderGoods.setOrderBusinessCode(orderBusiness.getOrderBusinessCode());
                        orderGoods.setGoodsCode(cartGoods.getGoodsCode());
                        orderGoods.setNumber(cartGoods.getNumber());
                        orderGoods.setPlanTime(new Date());
                        orderGoods.setGoodsName(goodsName);
                        orderGoods.setGoodsPrice(cartGoodsPrice.getPrice());
                        orderGoods.setGoodsImg(goodsHead);
                        orderGoodsService.insert(orderGoods);



                        //订单商品的属性
                        OrderGoodsPrice orderGoodsPrice = new OrderGoodsPrice();
                        orderGoodsPrice.setOrderGoodsPriceCode(UUID.randomUUID().toString());
                        orderGoodsPrice.setOrderGoodsCode(orderGoods.getOrderGoodsCode());
                        orderGoodsPrice.setPrice(cartGoodsPrice.getPrice());

                        if(cartGoods.getAddedType().equals("Added")){
                            String goodsSizeCode = cartGoodsPrice.getGoodsSizeCode();
                            String goodsSizeName = "";
                            if(!goodsSizeCode.equals("")){
                                goodsSizeName = goodsSizeService.getByGoodsSizeCode(goodsSizeCode).getName();
                            }
                            orderGoodsPrice.setGoodsSizeCode(goodsSizeCode);
                            orderGoodsPrice.setGoodsSizeName(goodsSizeName);

                            String goodsColorCode =cartGoodsPrice.getGoodsColorCode();
                            String goodsColorName = "";
                            if(!goodsColorCode.equals("")){
                                goodsColorName = goodsColorService.getByGoodsColorCode(goodsColorCode).getName();
                            }
                            orderGoodsPrice.setGoodsColorCode(goodsColorCode);
                            orderGoodsPrice.setGoodsColorName(goodsColorName);
                        }

                        orderGoodsPriceService.insert(orderGoodsPrice);


                    }

                }
            }



            if(payType.equals("yue")){

                //修改总订单的支付状态
                OrderTradeno orderTradeno1 = orderTradenoService.getByTradeno(tradeno);
                if(orderTradeno1.getStatus() == 0){
                    orderTradeno1.setStatus(1);
                    orderTradenoService.update(orderTradeno1);
                }


                //交易号关联的订单
                List<OrderBusinessTradenoRel> listOrderBusinessTradeoRel =
                        orderBusinessTradenoRelService.getListByTradeno(tradeno);

                //修改订单的状态
                for(OrderBusinessTradenoRel orderBusinessTradenoRel2:listOrderBusinessTradeoRel){
                    OrderBusiness orderBusiness2 =
                            orderBusinessService.getByOrderBusinessCode(orderBusinessTradenoRel2.getOrderBusinessCode());
                    if(orderBusiness2.getStatus() == 0){
                        orderBusiness2.setStatus(3);
                        orderBusiness2.setPayTime(new Date());
                        orderBusiness2.setCompleteTime(new Date());
                        orderBusinessService.update(orderBusiness2);
                    }
                }


                elderly.setBalance(elderly.getBalance()-orderTradeno1.getPrice());
                elderlyService.update(elderly);

            }



            UserPrinter userPrinter = userPrinterService.getByUserCode(userCode);
            String printerStr = getPrinterStr(order.getOrderCode());


            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("payType",payType);

            jsonObject1.put("printerStr",printerStr);
            jsonObject1.put("userPrinter",userPrinter);
            return success(jsonObject1);
        }
        catch (Exception e){
            insertErrorForAffair("/AppCashier/CartSubmitMember",jsonObject,e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return anomalous(getExceptionMsg(e));
        }

    }


    //收银员提交（支付宝）
    @RequestMapping(value = "CartSubmitAli",method = RequestMethod.POST)
    @Transactional
    public MyResult CartSubmitAli(@RequestBody JSONObject jsonObject, HttpServletRequest request) {

        try{

            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String userCode = jsonObject.getString("userCode");
            String businessCode = jsonObject.getString("businessCode");

            String authCode = jsonObject.getString("authCode");


            String payType = "ali";
            String clientType = "table";

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Cashier/CartSubmitAli");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }


            User user11 = userService.getByUserCode(userCode);
            String governmentCode = user11.getGovernmentCode();


            //支付交易号
            String tradeno = "sn"+ getTimeStrSSS()+ new Random().nextInt(100000);


            CashierCart cart = cashierCartService.getByUserCodeGovernmentCode(userCode,governmentCode);
            if(cart == null){
                return failed("购物车为空");
            }

            CashierCartBusiness cashierCartBusinessSelected = null;
            List<CashierCartBusiness> listCartBusiness =
                    cashierCartBusinessService.getListByCashierCartCodeWillSubmit(cart.getCashierCartCode());
            for(CashierCartBusiness cartBusiness : listCartBusiness){
                if(cartBusiness.getBusinessCode().equals(businessCode)){
                    cashierCartBusinessSelected = cartBusiness;
                }
            }
            if(cashierCartBusinessSelected == null){
                return failed("订单为空");
            }





            AlipayTradePayResponse response = AliPayUtils.barcodePay2(tradeno,
                    cashierCartBusinessSelected.getPrice()+"", "商品购买", authCode);

            //AlipayTradePayResponse response = AliPayUtils.barcodePay2(tradeno,
             //       "0.01", "subject", authCode);


            System.out.println(response.toString());

            if(!response.isSuccess()){
                return failed("支付失败");
            }




            //订单的支付交易号
            OrderTradeno orderTradeno = new OrderTradeno();
            orderTradeno.setOrderTradenoCode(UUID.randomUUID().toString());
            orderTradeno.setUserCode(userCode);
            orderTradeno.setTradeNo(tradeno);
            orderTradeno.setPrice(cashierCartBusinessSelected.getPrice());
            orderTradeno.setCreateTime(new Date());
            orderTradeno.setStatus(0);
            orderTradeno.setPayType(payType);
            orderTradeno.setClientType(clientType);
            orderTradenoService.insert(orderTradeno);


            //订单
            Order order = new Order();
            order.setOrderCode(UUID.randomUUID().toString());
            order.setUserCode(userCode);
            order.setPrice(cashierCartBusinessSelected.getPrice());
            order.setCreateTime(new Date());
            order.setReceiveName("");
            order.setReceivePhone("");
            order.setReceiveArea("");
            order.setReceiveAreaPath("");
            order.setReceiveAddress("");
            order.setGovernmentCode(governmentCode);
            order.setCashierElderlyCode(emptyUUID);
            orderService.insert(order);




            for(CashierCartBusiness cartBusiness : listCartBusiness){
                if(cartBusiness.getBusinessCode().equals(businessCode)){

                    cashierCartBusinessService.delete(cartBusiness.getCashierCartBusinessCode());


                    Business business = businessService.getByBusinessCode(cartBusiness.getBusinessCode());
                    User userBusiness = userService.getByUserCode(business.getUserCode());

                    //订单流水号
                    String serialNumber = getTimeStr() +user11.getPhone()+ new Random().nextInt(10);

                    //订单的商家
                    OrderBusiness orderBusiness = new OrderBusiness();
                    orderBusiness.setOrderBusinessCode(UUID.randomUUID().toString());
                    orderBusiness.setOrderCode(order.getOrderCode());
                    orderBusiness.setBusinessCode(cartBusiness.getBusinessCode());
                    orderBusiness.setBusinessName(userBusiness.getName());
                    orderBusiness.setPrice(cartBusiness.getPrice());
                    orderBusiness.setCreateTime(new Date());
                    orderBusiness.setOrderType(cartBusiness.getGoodsType());
                    orderBusiness.setStatus(0);
                    orderBusiness.setSerialNumber(serialNumber);
                    orderBusiness.setPayType(payType);

                    orderBusiness.setOrderFrom("table");

                    orderBusinessService.insert(orderBusiness);


                    //商家的订单与支付交易号关联
                    OrderBusinessTradenoRel orderBusinessTradenoRel = new OrderBusinessTradenoRel();
                    orderBusinessTradenoRel.setOrderBusinessTradeNoRelCode(UUID.randomUUID().toString());
                    orderBusinessTradenoRel.setTradeNo(tradeno);
                    orderBusinessTradenoRel.setOrderBusinessCode(orderBusiness.getOrderBusinessCode());
                    orderBusinessTradenoRelService.insert(orderBusinessTradenoRel);



                    double priceOrder = orderBusiness.getPrice();

                    List<CashierCartGoods> listCartGoods =
                            cashierCartGoodsService.getListByCashierCartBusinessCode(cartBusiness.getCashierCartBusinessCode());
                    for(CashierCartGoods cartGoods : listCartGoods){
                        cashierCartGoodsService.delete(cartGoods.getCashierCartGoodsCode());


                        String goodsName = "";
                        String goodsNameMore = "";
                        String goodsHead = "";

                        if(cartGoods.getAddedType().equals("Added")){
                            Goods goods = goodsService.getByGoodsCode(cartGoods.getGoodsCode());
                            goodsName = goods.getName();
                            goodsNameMore = goods.getNameMore();
                            goodsHead = goods.getHead1();

                            //增加库存
                            if(goods.getStock() > goods.getSaleCount()+cartGoods.getNumber()){
                                goods.setSaleCount(goods.getSaleCount()+cartGoods.getNumber());
                                goodsService.update(goods);
                            }

                        }

                        CashierCartGoodsPrice cartGoodsPrice =
                                cashierCartGoodsPriceService.getByCashierCartGoodsCode(cartGoods.getCashierCartGoodsCode());



                        //订单的商品
                        OrderGoods orderGoods = new OrderGoods();
                        orderGoods.setOrderGoodsCode(UUID.randomUUID().toString());
                        orderGoods.setOrderBusinessCode(orderBusiness.getOrderBusinessCode());
                        orderGoods.setGoodsCode(cartGoods.getGoodsCode());
                        orderGoods.setNumber(cartGoods.getNumber());
                        orderGoods.setPlanTime(new Date());
                        orderGoods.setGoodsName(goodsName);
                        orderGoods.setGoodsPrice(cartGoodsPrice.getPrice());
                        orderGoods.setGoodsImg(goodsHead);
                        orderGoodsService.insert(orderGoods);



                        //订单商品的属性
                        OrderGoodsPrice orderGoodsPrice = new OrderGoodsPrice();
                        orderGoodsPrice.setOrderGoodsPriceCode(UUID.randomUUID().toString());
                        orderGoodsPrice.setOrderGoodsCode(orderGoods.getOrderGoodsCode());
                        orderGoodsPrice.setPrice(cartGoodsPrice.getPrice());

                        if(cartGoods.getAddedType().equals("Added")){
                            String goodsSizeCode = cartGoodsPrice.getGoodsSizeCode();
                            String goodsSizeName = "";
                            if(!goodsSizeCode.equals("")){
                                goodsSizeName = goodsSizeService.getByGoodsSizeCode(goodsSizeCode).getName();
                            }
                            orderGoodsPrice.setGoodsSizeCode(goodsSizeCode);
                            orderGoodsPrice.setGoodsSizeName(goodsSizeName);

                            String goodsColorCode =cartGoodsPrice.getGoodsColorCode();
                            String goodsColorName = "";
                            if(!goodsColorCode.equals("")){
                                goodsColorName = goodsColorService.getByGoodsColorCode(goodsColorCode).getName();
                            }
                            orderGoodsPrice.setGoodsColorCode(goodsColorCode);
                            orderGoodsPrice.setGoodsColorName(goodsColorName);
                        }

                        orderGoodsPriceService.insert(orderGoodsPrice);


                    }

                }
            }


            if(payType.equals("ali")){

                //修改总订单的支付状态
                OrderTradeno orderTradeno1 = orderTradenoService.getByTradeno(tradeno);
                if(orderTradeno1.getStatus() == 0){
                    orderTradeno1.setStatus(1);
                    orderTradenoService.update(orderTradeno1);
                }


                //交易号关联的订单
                List<OrderBusinessTradenoRel> listOrderBusinessTradeoRel =
                        orderBusinessTradenoRelService.getListByTradeno(tradeno);

                //修改订单的状态
                for(OrderBusinessTradenoRel orderBusinessTradenoRel2:listOrderBusinessTradeoRel){
                    OrderBusiness orderBusiness2 =
                            orderBusinessService.getByOrderBusinessCode(orderBusinessTradenoRel2.getOrderBusinessCode());
                    if(orderBusiness2.getStatus() == 0){
                        orderBusiness2.setStatus(3);
                        orderBusiness2.setPayTime(new Date());
                        orderBusiness2.setCompleteTime(new Date());
                        orderBusinessService.update(orderBusiness2);
                    }
                }

            }



            UserPrinter userPrinter = userPrinterService.getByUserCode(userCode);
            String printerStr = getPrinterStr(order.getOrderCode());


            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("payType",payType);
            jsonObject1.put("printerStr",printerStr);
            jsonObject1.put("userPrinter",userPrinter);
            return success(jsonObject1);
        }
        catch (Exception e){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            insertErrorForAffair("/AppCashier/CartSubmitAli",jsonObject,e);
            return anomalous(getExceptionMsg(e));
        }

    }


    //收银员提交（微信）
    @RequestMapping(value = "CartSubmitWx",method = RequestMethod.POST)
    @Transactional
    public MyResult CartSubmitWx(@RequestBody JSONObject jsonObject, HttpServletRequest request) {

        try{

            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String userCode = jsonObject.getString("userCode");
            String businessCode = jsonObject.getString("businessCode");

            String authCode = jsonObject.getString("authCode");


            String payType = "wx";
            String clientType = "table";

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Cashier/CartSubmitWx");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }


            User user11 = userService.getByUserCode(userCode);
            String governmentCode = user11.getGovernmentCode();


            //支付交易号
            String tradeno = "sn"+ getTimeStrSSS()+ new Random().nextInt(100000);


            CashierCart cart = cashierCartService.getByUserCodeGovernmentCode(userCode,governmentCode);
            if(cart == null){
                return failed("购物车为空");
            }

            CashierCartBusiness cashierCartBusinessSelected = null;
            List<CashierCartBusiness> listCartBusiness =
                    cashierCartBusinessService.getListByCashierCartCodeWillSubmit(cart.getCashierCartCode());
            for(CashierCartBusiness cartBusiness : listCartBusiness){
                if(cartBusiness.getBusinessCode().equals(businessCode)){
                    cashierCartBusinessSelected = cartBusiness;
                }
            }
            if(cashierCartBusinessSelected == null){
                return failed("订单为空");
            }





            Map response = WechatPayUtils.barcodePay(authCode,tradeno, cashierCartBusinessSelected.getPrice());
            //Map response = WechatPayUtils.barcodePay(authCode,tradeno, 0.01);

            if(!response.get("tag").toString().equals("ok")){
                return failed("支付失败");
            }




            //订单的支付交易号
            OrderTradeno orderTradeno = new OrderTradeno();
            orderTradeno.setOrderTradenoCode(UUID.randomUUID().toString());
            orderTradeno.setUserCode(userCode);
            orderTradeno.setTradeNo(tradeno);
            orderTradeno.setPrice(cashierCartBusinessSelected.getPrice());
            orderTradeno.setCreateTime(new Date());
            orderTradeno.setStatus(0);
            orderTradeno.setPayType(payType);
            orderTradeno.setClientType(clientType);
            orderTradenoService.insert(orderTradeno);


            //订单
            Order order = new Order();
            order.setOrderCode(UUID.randomUUID().toString());
            order.setUserCode(userCode);
            order.setPrice(cashierCartBusinessSelected.getPrice());
            order.setCreateTime(new Date());
            order.setReceiveName("");
            order.setReceivePhone("");
            order.setReceiveArea("");
            order.setReceiveAreaPath("");
            order.setReceiveAddress("");
            order.setGovernmentCode(governmentCode);
            order.setCashierElderlyCode(emptyUUID);
            orderService.insert(order);




            for(CashierCartBusiness cartBusiness : listCartBusiness){
                if(cartBusiness.getBusinessCode().equals(businessCode)){

                    cashierCartBusinessService.delete(cartBusiness.getCashierCartBusinessCode());


                    Business business = businessService.getByBusinessCode(cartBusiness.getBusinessCode());
                    User userBusiness = userService.getByUserCode(business.getUserCode());


                    //订单流水号
                    String serialNumber = getTimeStr() +user11.getPhone()+ new Random().nextInt(10);


                    //订单的商家
                    OrderBusiness orderBusiness = new OrderBusiness();
                    orderBusiness.setOrderBusinessCode(UUID.randomUUID().toString());
                    orderBusiness.setOrderCode(order.getOrderCode());
                    orderBusiness.setBusinessCode(cartBusiness.getBusinessCode());
                    orderBusiness.setBusinessName(userBusiness.getName());
                    orderBusiness.setPrice(cartBusiness.getPrice());
                    orderBusiness.setCreateTime(new Date());
                    orderBusiness.setOrderType(cartBusiness.getGoodsType());
                    orderBusiness.setStatus(0);
                    orderBusiness.setSerialNumber(serialNumber);
                    orderBusiness.setPayType(payType);

                    orderBusiness.setOrderFrom("table");

                    orderBusinessService.insert(orderBusiness);


                    //商家的订单与支付交易号关联
                    OrderBusinessTradenoRel orderBusinessTradenoRel = new OrderBusinessTradenoRel();
                    orderBusinessTradenoRel.setOrderBusinessTradeNoRelCode(UUID.randomUUID().toString());
                    orderBusinessTradenoRel.setTradeNo(tradeno);
                    orderBusinessTradenoRel.setOrderBusinessCode(orderBusiness.getOrderBusinessCode());
                    orderBusinessTradenoRelService.insert(orderBusinessTradenoRel);



                    double priceOrder = orderBusiness.getPrice();

                    List<CashierCartGoods> listCartGoods =
                            cashierCartGoodsService.getListByCashierCartBusinessCode(cartBusiness.getCashierCartBusinessCode());
                    for(CashierCartGoods cartGoods : listCartGoods){
                        cashierCartGoodsService.delete(cartGoods.getCashierCartGoodsCode());


                        String goodsName = "";
                        String goodsNameMore = "";
                        String goodsHead = "";

                        if(cartGoods.getAddedType().equals("Added")){
                            Goods goods = goodsService.getByGoodsCode(cartGoods.getGoodsCode());
                            goodsName = goods.getName();
                            goodsNameMore = goods.getNameMore();
                            goodsHead = goods.getHead1();

                            //增加库存
                            if(goods.getStock() > goods.getSaleCount()+cartGoods.getNumber()){
                                goods.setSaleCount(goods.getSaleCount()+cartGoods.getNumber());
                                goodsService.update(goods);
                            }

                        }

                        CashierCartGoodsPrice cartGoodsPrice =
                                cashierCartGoodsPriceService.getByCashierCartGoodsCode(cartGoods.getCashierCartGoodsCode());



                        //订单的商品
                        OrderGoods orderGoods = new OrderGoods();
                        orderGoods.setOrderGoodsCode(UUID.randomUUID().toString());
                        orderGoods.setOrderBusinessCode(orderBusiness.getOrderBusinessCode());
                        orderGoods.setGoodsCode(cartGoods.getGoodsCode());
                        orderGoods.setNumber(cartGoods.getNumber());
                        orderGoods.setPlanTime(new Date());
                        orderGoods.setGoodsName(goodsName);
                        orderGoods.setGoodsPrice(cartGoodsPrice.getPrice());
                        orderGoods.setGoodsImg(goodsHead);
                        orderGoodsService.insert(orderGoods);



                        //订单商品的属性
                        OrderGoodsPrice orderGoodsPrice = new OrderGoodsPrice();
                        orderGoodsPrice.setOrderGoodsPriceCode(UUID.randomUUID().toString());
                        orderGoodsPrice.setOrderGoodsCode(orderGoods.getOrderGoodsCode());
                        orderGoodsPrice.setPrice(cartGoodsPrice.getPrice());

                        if(cartGoods.getAddedType().equals("Added")){
                            String goodsSizeCode = cartGoodsPrice.getGoodsSizeCode();
                            String goodsSizeName = "";
                            if(!goodsSizeCode.equals("")){
                                goodsSizeName = goodsSizeService.getByGoodsSizeCode(goodsSizeCode).getName();
                            }
                            orderGoodsPrice.setGoodsSizeCode(goodsSizeCode);
                            orderGoodsPrice.setGoodsSizeName(goodsSizeName);

                            String goodsColorCode =cartGoodsPrice.getGoodsColorCode();
                            String goodsColorName = "";
                            if(!goodsColorCode.equals("")){
                                goodsColorName = goodsColorService.getByGoodsColorCode(goodsColorCode).getName();
                            }
                            orderGoodsPrice.setGoodsColorCode(goodsColorCode);
                            orderGoodsPrice.setGoodsColorName(goodsColorName);
                        }

                        orderGoodsPriceService.insert(orderGoodsPrice);


                    }

                }
            }


            if(payType.equals("wx")){

                //修改总订单的支付状态
                OrderTradeno orderTradeno1 = orderTradenoService.getByTradeno(tradeno);
                if(orderTradeno1.getStatus() == 0){
                    orderTradeno1.setStatus(1);
                    orderTradenoService.update(orderTradeno1);
                }


                //交易号关联的订单
                List<OrderBusinessTradenoRel> listOrderBusinessTradeoRel =
                        orderBusinessTradenoRelService.getListByTradeno(tradeno);

                //修改订单的状态
                for(OrderBusinessTradenoRel orderBusinessTradenoRel2:listOrderBusinessTradeoRel){
                    OrderBusiness orderBusiness2 =
                            orderBusinessService.getByOrderBusinessCode(orderBusinessTradenoRel2.getOrderBusinessCode());
                    if(orderBusiness2.getStatus() == 0){
                        orderBusiness2.setStatus(3);
                        orderBusiness2.setPayTime(new Date());
                        orderBusiness2.setCompleteTime(new Date());
                        orderBusinessService.update(orderBusiness2);
                    }
                }

            }



            UserPrinter userPrinter = userPrinterService.getByUserCode(userCode);
            String printerStr = getPrinterStr(order.getOrderCode());


            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("payType",payType);

            jsonObject1.put("printerStr",printerStr);
            jsonObject1.put("userPrinter",userPrinter);
            return success(jsonObject1);
        }
        catch (Exception e){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            insertErrorForAffair("/AppCashier/CartSubmitWx",jsonObject,e);
            return anomalous(getExceptionMsg(e));
        }

    }




    /**
     * order------------------
     * */

    //收银员订单列表
    @RequestMapping(value = "ListOrderBusiness",method = RequestMethod.POST)
    public MyResult ListOrderBusiness(@RequestBody JSONObject jsonObject) {

        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String userCode = jsonObject.getString("userCode");
            int pageIndex = jsonObject.getInteger("pageIndex");
            int pageSize = jsonObject.getInteger("pageSize");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Cashier/ListOrderBusiness");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }


            PageInfo<OrderBusinessDTO> pageInfoOrderBusinessDTO =
                    orderBusinessService.getPageByUserCodeTable(pageIndex, pageSize, userCode);

            List<OrderBusinessDTO> listOrderBusinessDTO = pageInfoOrderBusinessDTO.getList();
            for (OrderBusinessDTO orderBusinessDTO : listOrderBusinessDTO) {
                List<OrderGoodsDTO> listOrderGoodsDTO = orderGoodsService.getListByOrderBusinessCode(orderBusinessDTO.getOrderBusinessCode());
                for (OrderGoodsDTO orderGoodsDTO : listOrderGoodsDTO) {

                    //GoodsDTO goodsDTO = goodsService.getByGoodsCode(orderGoodsDTO.getGoodsCode());
                    //orderGoodsDTO.setGoods(goodsDTO);

                    OrderGoodsPrice orderGoodsPrice = orderGoodsPriceService.getByOrderGoodsCode(orderGoodsDTO.getOrderGoodsCode());
                    orderGoodsDTO.setOrderGoodsPrice(orderGoodsPrice);

                }
                orderBusinessDTO.setListOrderGoodsDTO(listOrderGoodsDTO);
            }


            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("pageInfoOrderBusinessDTO",pageInfoOrderBusinessDTO);
            return success(jsonObject1);
        }catch (Exception e){
            insertError("/AppCashier/ListOrderBusiness",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }

    }










    /**
     * function------------------
     * */

    //获取订单的打印字符串
    private String getPrinterStr(String orderCode){
        String rv = "";

        Order order = orderService.getByOrderCode(orderCode);
        OrderBusiness orderBusiness = orderBusinessService.getListByOrderCode(orderCode).get(0);
        User user = userService.getByUserCode(order.getUserCode());

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");


        rv += "收银："+user.getName()+"("+user.getPhone()+")\n";
        rv += "单号：" + orderBusiness.getSerialNumber() + "\n";
        rv += "时间："+sdf.format(orderBusiness.getCreateTime())+"\n";
        rv += "------------------------\n";


        rv += getSameLengthStr("商品名称",8);
        rv += getSameLengthStr("单价",3);
        rv += getSameLengthStr("数",2);
        rv += getSameLengthStr("小计",2);
        rv += "\n";

        List<OrderGoodsDTO> listOrderGoodsDTO =
                orderGoodsService.getListByOrderBusinessCode(orderBusiness.getOrderBusinessCode());
        for(OrderGoodsDTO orderGoodsDTO:listOrderGoodsDTO){
            OrderGoodsPrice orderGoodsPrice =
                    orderGoodsPriceService.getByOrderGoodsCode(orderGoodsDTO.getOrderGoodsCode());

            String goodsName = orderGoodsDTO.getGoodsName();
            String sizeName = orderGoodsPrice.getGoodsSizeName();
            String colorName = orderGoodsPrice.getGoodsColorName();

            String partName = "";
            if(!sizeName.equals("null")){
                partName += sizeName;
            }
            if(!colorName.equals("null")){
                if(partName.equals("")){
                    partName = colorName;
                }
                else{
                    partName += ","+colorName;
                }
            }

            String title = "";
            if(partName.equals("")){
                title = goodsName;
            }
            else{
                title = goodsName+"("+partName+")";
            }

            int l = title.length();

            if(l<=8){

                rv += getSameLengthStr(title,8);
                rv += getSameLengthStrChar(orderGoodsDTO.getGoodsPrice()+"",6);
                rv += getSameLengthStrChar(orderGoodsDTO.getNumber()+"",4);
                rv += getSameLengthStrChar(orderGoodsDTO.getGoodsPrice()*orderGoodsDTO.getNumber()+"",6);
                rv += "\n";

            }
            else{

                rv += getSameLengthStr(title.substring(0,8),8);
                rv += getSameLengthStrChar(orderGoodsDTO.getGoodsPrice()+"",6);
                rv += getSameLengthStrChar(orderGoodsDTO.getNumber()+"",4);
                rv += getSameLengthStrChar(orderGoodsDTO.getGoodsPrice()*orderGoodsDTO.getNumber()+"",6);
                rv += "\n";

                int k = title.length()>=16?16:title.length();
                String title2 = title.substring(8,k);
                rv += title2+"\n";

            }

        }


        String payTypeTitle = "";
        if(orderBusiness.getPayType().equals("cash")){
            payTypeTitle = "现金";
        }
        if(orderBusiness.getPayType().equals("yue")){
            payTypeTitle = "余额";
        }
        if(orderBusiness.getPayType().equals("wx")){
            payTypeTitle = "微信";
        }
        if(orderBusiness.getPayType().equals("ali")){
            payTypeTitle = "支付宝";
        }


        rv += "------------------------\n";
        rv += "现价：" + orderBusiness.getPrice()+ "\t支付类型：" + payTypeTitle + "\n";

        if(orderBusiness.getPayType().equals("yue")){
            String elderlyCode = order.getCashierElderlyCode();
            Elderly elderly = elderlyService.getByElderlyCode(elderlyCode);
            if(elderly != null){
                rv += "会员："+elderly.getName()+"\t账户余额："+String.format("%.2f", elderly.getBalance());
            }
        }

        rv += "------------------------\n";


        return rv;
    }


    //获取字符串中汉字的个数
    private int getHanZiCount(String str){
        String text = str;//"你好，,.。wo";
        String Reg="^[\u4e00-\u9fa5]{1}$";//正则
        int result=0;
        for(int i=0;i<text.length();i++){
            String b=Character.toString(text.charAt(i));
            if(b.matches(Reg))
                result++;
        }
        return result;
    }

    /**
     * 获取总长度汉字相等的字符串，如果字符串不够，用星号补齐，
     * */
    private String getSameLengthStr(String str,int allHZNumber){
        int hzLen = getHanZiCount(str);
        int otherLen = str.length()-hzLen;
        int starLen = allHZNumber-str.length();

        String rv = "";
        if(starLen<0){
            rv = str;
        }
        else{
           rv = str + getStartStr(otherLen)+getStartStr(starLen*2);
        }

        return rv;
    }

    /**
     * 获取总长度字符相等的字符串，如果字符串不够，用星号补齐，
     * */
    private String getSameLengthStrChar(String str,int allCharNumber){

        String rv = "";
        if(str.length()>allCharNumber){
            rv = str;
        }
        else{
            rv = str + getStartStr(allCharNumber-str.length());
        }

        return rv;
    }

    //获取几个星星的字符串
    private String getStartStr(int number){
        String rv = "";
        for(int i=0;i<number;i++){
            //rv += "*";
            rv += " ";
        }
        return rv;
    }



    //判断购物车中给定属性的商品是否已存在
    protected Boolean hadCashierCartGoodsPriceExist(String cashierCartGoodsCode,
                                                    JSONArray goodsAttributeCodesArg,
                                                    JSONArray goodsAttributeCodesValueArg){


        CashierCartGoodsPrice cashierCartGoodsPrice =
                cashierCartGoodsPriceService.getByCashierCartGoodsCode(cashierCartGoodsCode);


        //存储完全匹配的记录的标识
        Boolean bTop = true;

        //变量属性Code数组
        for(int i=0;i<goodsAttributeCodesArg.size();i++){

            //检查本条记录是否存在该键值
            Boolean b = haveThis3(cashierCartGoodsPrice,goodsAttributeCodesArg.getString(i),
                    goodsAttributeCodesValueArg.getString(i));


            //不存在该键值
            if(!b){

                //完全匹配的记录的标识设为false
                bTop = false;

                //退出剩余的检查
                break;
            }
        }


        return bTop;

    }

    //本条记录里是否存在该键值
    private Boolean haveThis3(CashierCartGoodsPrice cartGoodsPrice, String attributeCode, String attributeValue){


        //首字母改成大写
        attributeCode = attributeCode.substring(0, 1).toUpperCase()+ attributeCode.substring(1);


        //循环父类的属性
        Class superClass = cartGoodsPrice.getClass().getSuperclass();
        while (superClass != null) {
            Field[] superFileds = superClass.getDeclaredFields();
            for(int i=0;i< superFileds.length;i++){
                String name = superFileds[i].getName();
                Object obj = getFieldValueByName(name,cartGoodsPrice);
                String value = obj == null?"":obj.toString();
                if(name.equals(attributeCode) && value.equals(attributeValue)){
                    return true;
                }

            }
            superClass = superClass.getSuperclass();
        }

        //循环本类的属性
        Field[] allFields2 = cartGoodsPrice.getClass().getDeclaredFields();
        for(int i=0;i< allFields2.length;i++){
            String name = allFields2[i].getName();
            Object obj = getFieldValueByName(name,cartGoodsPrice);
            String value = obj == null?"":obj.toString();

            if(name.equals(attributeCode) && value.equals(attributeValue)){
                return true;
            }

        }


        return false;
    }



}
